package com.jianbo.proxy.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.smartboot.http.common.logging.Logger;
import org.smartboot.http.common.logging.LoggerFactory;

import com.jianbo.proxy.conf.ArgsConf;
import com.jianbo.proxy.conf.jsconfig.StaticWebConf;

// 提供给 js 调用的 api
public class JavaApi {

    private static Logger logger = LoggerFactory.getLogger(JavaApi.class);

    public static String truePath(String path) {
        return PathUtil.truePath(path);
    }

    public static String getFrontRoot() {
        ArgsConf argsConf = ArgsConf.getInstance();
        return argsConf.getFrontDir();
    }

    public static String getConfRoot() {
        ArgsConf argsConf = ArgsConf.getInstance();
        return argsConf.getConfDir();
    }
    
    public static String read(String path) {
        String truePath = truePath(path);
        try {
            String content = null;
            if(!truePath.equals(path)) {
                path = truePath;
                if(!FileUtil.exist(path)) {
                    throw new RuntimeException("路径：" + path + " 不存在");
                }
                content = FileUtil.readString(path);
            } else {
                if(path.indexOf(":") == 1) {
                    content = FileUtil.readString(path);
                }

                if(content == null) {
                    // 从classpath中读取
                    try {
                        content = ResourceUtil.readString(path);
                    } catch (Exception e) {
                        logger.info("class读取为空" + path);
                    }
                }
                
                // 从全局路径中读取 
                if(content == null) {
                    content = FileUtil.readString(path);
                }
            }

            return content;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            logger.error("ReadFileError：" + path);
            throw new RuntimeException("ReadFileError：" + path);
        }
    }

    public static String uuid() {
        return CommonUtil.newUUID();
    }

    public static String folders(String folder) {
        folder = truePath(folder);
        File folderFile = new File(folder);
        List<String> list = new ArrayList<>();
        if (folderFile.isDirectory()) {
            File[] files = folderFile.listFiles();
            for(File file : files) {
                list.add(file.getName());
            }
        }
        return JsonUtil.toJson(list);
    }

    public static String files(String folder) {
        folder = truePath(folder);
        List<String> list = FileUtil.traverseFiles(folder);
        List<String> list2 = new ArrayList<>();
        for(String path : list) {
            path = path.replaceAll("\\\\", "/");
            list2.add(path);
        }
        return JsonUtil.toJson(list2);
    }
    
    public static String resourceToUrl(String resourceUrl) {
        Map<String, String> path = StaticWebConf.getInstance().getStaticPath();
        for(String key : path.keySet()) {
            String resourcePath = path.get(key);
            if(resourceUrl.startsWith(resourcePath)) {
                return resourceUrl.replace(resourcePath, "/");
            }
        }
        logger.error(JsonUtil.toJson(path));
        throw new RuntimeException("找不到对应请求url：" + resourceUrl);
    }

    private static Map<String, String> data =new HashMap<>();

    public static void putGlobalData(String key, String value) {
        data.put(key, value);
    }

    public static String getGlobalData(String key) {
        return data.get(key);
    }

    public static void putData(String key, String value) {
        ThreadDataUtil.put(key, value);;
    }

    public static String getData(String key) {
        return ThreadDataUtil.get(key);
    }

    public static void pause() {
        Debug.info("JavaApi.pause Api Call");
        Debug.sleep(100000000);
    }
}
